$(function () {
    $("#storage_datagrid").datagrid({
        fit: true,
        url: '/guaranteebill/queryBill?state=1',
        columns: [[
            {field: 'id', title: 'id', width: 10, hidden: true}, //hidden 隐藏显示该列
            {field: 'number', title: '保险单号', width: 10},//允许列改变大小
            {
                field: 'applicant', title: '投保人', width: 10,
                formatter: function (value, row, index) {
                    return value.name;
                }
            },
            {field: 'vdate', title: '申请日期', width: 10},
            {field: 'limityear', title: '保险时长', width: 10},
            {
                field: 'op', title: '业务员', width: 10,
                formatter: function (value, row, index) {
                    console.log(value)
                    return value.username;
                }
            },
            {field: 'totalprice', title: '投保总金额', width: 10}
        ]],
        rownumbers: true,
        striped: true,
        toolbar: "#storage_toolbar",
        pagination: true,
        fitColumns: true,
        singleSelect: true
    })


    //高级查询
    $("#query").click(function () {
        var applicant = $("#applicant").textbox("getValue");
        var number = $("#number").textbox("getValue");
        $("#storage_datagrid").datagrid("load", {
            applicant: applicant,
            number: number
        })
    })
    //刷新
    $("#load").click(function () {
        $("#storage_datagrid").datagrid("reload");
    })

    //查看投保人明细
    $("#client_dialog").dialog({
        title: '正式客户',
        width: 300,
        height: 300,
        buttons: "#sure",
        closed: "true"
    })

    //打开投保人的窗口
    $("#client").click(function () {
        var row = $("#storage_datagrid").datagrid("getSelected");
        console.log(row);
        if (!row) {
            $.messager.alert("温馨提示", "至少选择一条数据");
        } else {
            var id = row.applicant.id;
            $("#client_form").form("load", "/latentClient/queryClientById?id=" + id);
            $("#client_dialog").dialog("open");
        }
    })


    //提审
    $("#audit").click(function () {
        var row = $("#storage_datagrid").datagrid("getSelected");
        console.log(row);
        if (!row) {
            $.messager.alert("温馨提示", "至少选择一条数据");
        } else {
            var id = row.id;
            $.messager.confirm('提审','您确认想要提审这条记录吗？',function(r){
                if(r){
                    $.get("/guaranteebill/auditClientById",{id:id},function (data) {
                        if(data){
                            $("#storage_datagrid").datagrid("reload");
                            $("#storage_datagrid").datagrid("unselectAll");
                        }else{
                            $.messager.alert("提审操作", "提审失败", "error");
                        }
                    });
                }
            });
        }
    })



    //打开保单详情
    $("#bill").click(function () {
        var row = $("#storage_datagrid").datagrid("getSelected");
        if (!row) {
            $.messager.alert("温馨提示", "至少选择一条数据");
        } else {
            var id = row.id;
            //$("#car_form").form("load", "/guaranteebill/queryClientById?id=" + id);
            $("#bill_dialog").dialog("open");
        }
    })

    //查看保单明细
    $("#bill_dialog").dialog({
        title: '查看保单信息',
        width: 300,
        height: 300,
        buttons: "#sure",
        closed: "true"
    })




    $("#showBill").click(function () {
        //编辑方法
        var row = $("#storage_datagrid").datagrid("getSelected");
        if (!row) {
            $.messager.alert("温馨提示", '至少选择一条数据', 'warning');
            return;
        }
        console.log(row)
        var carId = row.carinfo.id;
        console.log(carId)
        $("#carinfo_form").form("load", "/guaranteebill/queryCarInfo?carId="+carId)
        $("#edit_dialog").dialog('open');
        $("#edit_dialog").dialog('center');

    });
    //初始化弹窗
    $("#edit_dialog").dialog({
        closed: true,
        title: '查看保单信息',
        width: 700,
        height: 600,
        buttons: '#showInfo_buttons',
        top: 200
    });

    //点击关闭事件
    $("#showInfo_cancel").click(function () {
        $("#edit_dialog").dialog("close");
    });
})











